package com.brainardphotography.blog;

import javax.persistence.Id;

import com.google.appengine.api.users.User;

public class BlogUser {
	@Id
	private String userId;
	private String email;
	private String nickname;

	private boolean admin = false;

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getNickname() {
		return nickname;
	}

	public void setNickname(String nickname) {
		this.nickname = nickname;
	}

	public boolean isAdmin() {
		return admin;
	}

	public void setAdmin(boolean admin) {
		this.admin = admin;
	}

	public static Builder withBuilder() {
		return new Builder(new BlogUser());
	}

	public static Builder withAppengineUser(User user) {
		String id = user.getUserId() != null ? user.getUserId() :
			user.getFederatedIdentity() != null ? user.getFederatedIdentity() :
				user.getEmail();
			
		return withBuilder()
				.userId(id)
				.nickname(user.getNickname())
				.email(user.getEmail());
	}

	public static class Builder {
		private BlogUser user;

		private Builder(BlogUser user) {
			this.user = user;
		}
		
		public Builder userId(String userId) {
			user.userId = userId;
			return this;
		}
		
		public Builder email(String email) {
			user.email = email;
			return this;
		}
		
		public Builder nickname(String nickname) {
			user.nickname = nickname;
			return this;
		}
		
		public Builder admin(boolean admin) {
			user.admin = admin;
			return this;
		}
		
		public BlogUser build() {
			return this.user;
		}
	}
	
	public String toString() {
		StringBuilder b = new StringBuilder();

		b.append(getClass().getName()).append("[")
			.append("userId=").append(userId)
			.append(", email=").append(email)
			.append(", nickname=").append(nickname)
			.append("]");
		
		return b.toString();
	}
}
